package com.xiaoqi.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xiaoqi.constant.MessageConstant;
import com.xiaoqi.pojo.PageResult;
import com.xiaoqi.pojo.QueryPageBean;
import com.xiaoqi.pojo.Result;
import com.xiaoqi.pojo.Setmeal;
import com.xiaoqi.service.SetMealService;
import com.xiaoqi.utils.QiNiuUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import java.util.UUID;

/**
 * @author PC
 * creationTime: 2022/10/20 14:05
 */


@RequestMapping("/setmeal")
@RestController
public class SetMealController {

    @Reference
    private SetMealService setMealService;

    /**
     * 根据条件分页查询
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(
            @RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
            @RequestParam(value = "queryString",defaultValue = "") String queryString
    ){
        PageResult pageResult= setMealService.findAll(new QueryPageBean(currentPage, pageSize, queryString));
        return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS,pageResult);
    }

    /**
     * 根据套餐'id'删除套餐
     * @param id
     */
    @DeleteMapping("/del")
    public Result del(@RequestParam(name = "id") Integer id){
        setMealService.del(id);
        return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

    /**
     * 上传图片到七牛云
     * @param imgFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile) throws IOException {
        //获取文件的全名称截取后缀来组成新的文件名,通过查找最后一个点来截取点后的内容来确定后缀
        String suffix = imgFile.getOriginalFilename().substring(imgFile.getOriginalFilename().lastIndexOf("."));
        //随机生成一个唯一的名字来避免名称重复
        UUID uuid = UUID.randomUUID();
        //新文件名
        String FileName = uuid+suffix;
        //将图片上传至七牛云
        QiNiuUtils.uploadViaByte(imgFile.getBytes(),FileName);
        //需要返回两部分数据所有为了方便使用map
        HashMap<String, Object> map = new HashMap<>();
        map.put("filename",FileName);
        map.put("domian", QiNiuUtils.DOMAIN);
        return new Result(true,MessageConstant.PIC_UPLOAD_FAIL,map);
    }

    /**
     * 新增套餐
     * @param setmeal
     * @param checkgroupIds
     */
    @PostMapping("/addsetmeal")
    public Result addsetmeal(@RequestBody Setmeal setmeal,Integer [] checkgroupIds) {
        setMealService.addSetMeal(setmeal,checkgroupIds);
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 通过id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/findbyid")
    public Result findById(@RequestParam(value = "id")Integer id){
        Setmeal setmeal = setMealService.findById(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("setmeal",setmeal);
        map.put("domain",QiNiuUtils.DOMAIN);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,map);
    }


    /**
     * 查询套餐关联的所有检查组的id
     * @param id
     * @return
     */
    @GetMapping("/findSetMeal_CheckGroupByid")
    public Result findSetMeal_CheckGroupByid(@RequestParam(value = "id")Integer id){
        List<Integer> list = setMealService.findSetMeal_CheckGroupByid(id);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
    }

    /**
     * 编辑套餐
     * @param setmeal
     * @param checkgroupIds
     */
    @PutMapping("/update")
    public Result update(@RequestBody Setmeal setmeal,Integer [] checkgroupIds){
        setMealService.update(setmeal,checkgroupIds);
        return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
    }
}
